package com.lianjing.sh.acceleo.controller.mvc.controller.io.output;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.uml2.uml.NamedElement;
import org.hamcrest.CoreMatchers;
import org.hamcrest.MatcherAssert;
import org.junit.Assert;
import org.junit.Test;

import com.lianjing.sh.acceleo.controller.tool.service.NameService;

public class Name_Output_Service{

	private static final NameService Name_Service = new NameService();
	
	private static final String Ajax = "Ajax";
	private static final String Excel = "Excel";
	private static final String DateDefaultFormat = "yy/MM/dd HH:mm:ss";
	
	/**
	 * 后缀：
	 * :o{} : 输出  abc:o{yes}{a e}{yy/MM/dd HH:mm:ss}
	 */
	private static String Name1 = "abc:o{false}{a e}{yy/MM/dd HH:mm:ss}";
	private static String Name2 = "abc:o{}{e}{yy/MM/dd HH:mm:ss}";
	private static String Name3 = "abc:o{}{a}{yy/MM/dd}";
	private static String name4 = "abc:o{}{}{}";
	private static String name5 = "abc:o{}{}";
	private static String name6 = "abc:o{}";
	private static String name7 = "abc:o";
	private static String name8 = "abc";

	/**
	 * ( abc ) abc:o{true}{a e}{yy/MM/dd HH:mm:ss} -> true
	 */
	public Boolean isOutputOrder(NamedElement element){
		return isOutputOrder(element.getName());
	}
	public Boolean isOutputOrder(String name){
		if(null == name){
			return true;
		}
		if(!Name_Service.hasNameAttr(name, "o")){
			return true;
		}
		List<String> paras = Name_Service.getNameAttrParas(name, "o");
		if(!paras.isEmpty()){
			String value = paras.get(0);
			if("".equals(value)){
				return true;
			}
			return Boolean.parseBoolean(value);
		}
		return true;
	}
	@Test
	public void testIsOutputOrder() {
		Name_Output_Service service = new Name_Output_Service();
		Assert.assertEquals(false, service.isOutputOrder(Name1));
		Assert.assertEquals(true, service.isOutputOrder(Name2));
		Assert.assertEquals(true, service.isOutputOrder(Name3));
		Assert.assertEquals(true, service.isOutputOrder(name4));
		Assert.assertEquals(true, service.isOutputOrder(name5));
		Assert.assertEquals(true, service.isOutputOrder(name6));
		Assert.assertEquals(true, service.isOutputOrder(name7));
		Assert.assertEquals(true, service.isOutputOrder(name8));
	}
	
	/**
	 * ( abc ) abc:o{true}{a e}{yy/MM/dd HH:mm:ss} -> true
	 */
	public List<String> getOutputType(NamedElement element){
		return getOutputType(element.getName());
	}
	public List<String> getOutputType(String name){
		List<String> array = new ArrayList<>();
			array.add(Ajax);
			array.add(Excel);
		if(null == name){
			return array;
		}
		if(!Name_Service.hasNameAttr(name, "o")){
			return array;
		}
		List<String> paras = Name_Service.getNameAttrParas(name, "o");
		if(paras.size()>1){
			String value = paras.get(1);
			if("".equals(value)){
				return array;
			}else{
				array = new ArrayList<>();
				for(String item : value.split(" ")){
					item = item.trim();
					if(item.equals("a")){
						array.add(Ajax);
					}else if(item.equals("e")){
						array.add(Excel);
					}
				}
			}
		}
		return array;
	}
	@Test
	public void testGetOutputType() {
		Name_Output_Service service = new Name_Output_Service();
		MatcherAssert.assertThat(service.getOutputType(Name1), CoreMatchers.hasItems(Ajax, Excel));
		MatcherAssert.assertThat(service.getOutputType(Name1).size(), CoreMatchers.is(2));
		
		MatcherAssert.assertThat(service.getOutputType(Name2), CoreMatchers.hasItems(Excel));
		MatcherAssert.assertThat(service.getOutputType(Name2).size(), CoreMatchers.is(1));
		
		MatcherAssert.assertThat(service.getOutputType(Name3), CoreMatchers.hasItems(Ajax));
		MatcherAssert.assertThat(service.getOutputType(Name3).size(), CoreMatchers.is(1));
		
		MatcherAssert.assertThat(service.getOutputType(name4), CoreMatchers.hasItems(Ajax, Excel));
		MatcherAssert.assertThat(service.getOutputType(name4).size(), CoreMatchers.is(2));
		
		MatcherAssert.assertThat(service.getOutputType(name5), CoreMatchers.hasItems(Ajax, Excel));
		MatcherAssert.assertThat(service.getOutputType(name5).size(), CoreMatchers.is(2));
		
		MatcherAssert.assertThat(service.getOutputType(name6), CoreMatchers.hasItems(Ajax, Excel));
		MatcherAssert.assertThat(service.getOutputType(name6).size(), CoreMatchers.is(2));
		
		MatcherAssert.assertThat(service.getOutputType(name7), CoreMatchers.hasItems(Ajax, Excel));
		MatcherAssert.assertThat(service.getOutputType(name7).size(), CoreMatchers.is(2));
		
		MatcherAssert.assertThat(service.getOutputType(name8), CoreMatchers.hasItems(Ajax, Excel));
		MatcherAssert.assertThat(service.getOutputType(name8).size(), CoreMatchers.is(2));
	}
	
	/**
	 * ( abc ) abc:o{true}{a e}{yy/MM/dd HH:mm:ss} -> yy/MM/dd HH:mm:ss
	 */
	public String getOutputDateFormat(NamedElement element){
		return getOutputDateFormat(element.getName());
	}
	public String getOutputDateFormat(String name){
		if(null == name){
			return DateDefaultFormat;
		}
		if(!Name_Service.hasNameAttr(name, "o")){
			return DateDefaultFormat;
		}
		List<String> paras = Name_Service.getNameAttrParas(name, "o");
		if(paras.size()>2){
			String value = paras.get(2);
			if("".equals(value)){
				return DateDefaultFormat;
			}
			return value;
		}
		return DateDefaultFormat;
	}
	@Test
	public void testGetOutputDateFormat() {
		Name_Output_Service service = new Name_Output_Service();
		Assert.assertEquals(DateDefaultFormat, service.getOutputDateFormat(Name1));
		Assert.assertEquals(DateDefaultFormat, service.getOutputDateFormat(Name2));
		Assert.assertEquals("yy/MM/dd", service.getOutputDateFormat(Name3));
		Assert.assertEquals(DateDefaultFormat, service.getOutputDateFormat(name4));
		Assert.assertEquals(DateDefaultFormat, service.getOutputDateFormat(name5));
		Assert.assertEquals(DateDefaultFormat, service.getOutputDateFormat(name6));
		Assert.assertEquals(DateDefaultFormat, service.getOutputDateFormat(name7));
		Assert.assertEquals(DateDefaultFormat, service.getOutputDateFormat(name8));
	}
	
}
